package com.sky.mapper;

import com.sky.entity.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;

/**
 * C端用户数据访问层接口
 *
 * @Author Vsunks.v
 * @Date 2023/5/6 15:38
 * @Blog blog.sunxiaowei.net
 * @Description:
 */
public interface UserMapper {

    /**
     * 通过openid查询用户
     *
     * @param openid
     * @return
     */
    @Select("select * from user where openid= #{openid};")
    User selectByOpenId(@Param("openid") String openid);

    /**
     * 新增用户
     *
     * @param user
     */
    @Insert("insert into user (openid, name, phone, sex, id_number, avatar, create_time)" +
            "        values (#{openid}, #{name}, #{phone}, #{sex}, #{idNumber}, #{avatar}, #{createTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(User user);

    /**
     * 根据id查询用户
     *
     * @param id 用户id
     * @return
     */
    @Select("select * from user where id = #{id}")
    User selectById(Long id);

    /**
     * 查询截止到指定日期的总用户数
     * @param date 指定日期
     * @return  数量
     */
    @Select("select count(*) from user where date (create_time) <= #{date}")
    int countAllByCreateDate(LocalDate date);

    /**
     * 查询指定日期新增用户数
     * @param date 指定日期
     * @return 数量
     */
    @Select("select count(*) from user where date (create_time) = #{date}")
    int countNewByCreateDate(LocalDate date);
}
